package com.dragon.user_service.api.LogService;

import com.dragon.user_service.api.MQ.MQConstant;
import com.dragon.user_service.api.MQ.writeLogEvent;
import com.dragon.user_service.domain.entity.OperationLogs;
import com.dragon.user_service.service.OperationLogsService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@RocketMQMessageListener(topic = MQConstant.LOG_MSG_TOPIC, consumerGroup = MQConstant.LOG_MSG_GROUP)
@Component
public class LogConsumer implements RocketMQListener<writeLogEvent> {
    @Resource
    private OperationLogsService operationLogsService;
    @Override
    @Transactional
    public void onMessage(writeLogEvent event) {
        //持久化日志
        OperationLogs log = new OperationLogs();
        log.setUser_id(event.getUser_id());
        log.setAction(event.getAction());
        log.setIp(event.getIp());
        log.setDetail(event.getDetail()); // 记录操作详情（如修改字段）
        operationLogsService.save(log);
    }
}
